﻿Imports Microsoft.VisualBasic
Imports System.Data
Imports System
Partial Public Class Service
#Region "基础资料管理"
    Public Function _BILL_Water_ManageBase(ByVal FSession As String, ByRef FCinfo As FBaseWater, ByRef ErrString As String) As Boolean Implements IService._BILL_Water_ManageBase
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            If FCinfo Is Nothing Then
                ErrString = "参数不正确"
                Return False
            End If
            Dim sqlstr As String = String.Empty
            Dim ds As DataSet = Nothing
            If FCinfo.id > 0 And FCinfo.FName <> String.Empty Then
                '--更新
                sqlstr = String.Format("update Base_Water set FName='{0}',FCName='{1}',FRate={2},FUse=FUse+{3},FBalance={4} where id={5}", FCinfo.FName, FCinfo.FCName, FCinfo.FRate, FCinfo.FUse, FCinfo.FBalance, FCinfo.id)
                Return DControl.ExecSQL(sqlstr)
            ElseIf FCinfo.id = -1 Then
                '--新增
                sqlstr = String.Format("select * from Base_Water where FName='{0}'", FCinfo.FName)
                ds = New DataSet
                If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                    Return False
                End If
                If ds.Tables(0).Rows.Count > 0 Then
                    ErrString = "客户已经存在!"
                    Return False
                End If
                sqlstr = String.Format("INSERT INTO Base_Water(FName,FCName,FRate,FUse,FBalance) values('{0}','{1}','{2}','{3}','{4}')", FCinfo.FName, FCinfo.FCName, FCinfo.FRate, FCinfo.FUse, FCinfo.FBalance)
                If DControl.ExecSQL(sqlstr, ErrString) = False Then
                    Return False
                End If
                '--取ID
                sqlstr = String.Format("select * from Base_Water where FName='{0}'", FCinfo.FName)
                ds = New DataSet
                If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                    Return False
                End If
                If ds.Tables(0).Rows.Count < 1 Then
                    ErrString = "未知错误"
                    Return False
                End If
                FCinfo.id = ds.Tables(0).Rows(0).Item("id")
                Return True
            Else
                ErrString = "参数不正确"
            End If
            Return True
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region

#Region "基础资料查询"
    Public Function _BILL_Water_GetCustomList(ByVal FSession As String, ByVal FStyle As String, ByRef DsArr As Byte(), ByRef ErrString As String) As Boolean Implements IService._BILL_Water_GetCustomList
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            Dim sqlstr As String = String.Empty
            Dim ds As DataSet = Nothing
            Select Case FStyle
                Case "*"
                    sqlstr = "select * from Base_Water"
                Case Else
                    ErrString = "参数错误"
                    Return False
            End Select
            ds = New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            DsArr = FControl.CvtDs(ds)

            Return True
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region

#Region "获取基础资料信息"
    Public Function _BILL_Water_GetCustInfo(ByVal FSession As String, ByRef FCInf As FBaseWater, ByRef ErrString As String) As Boolean Implements IService._BILL_Water_GetCustInfo
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            Dim sqlstr As String = String.Empty
            Dim ds As DataSet = Nothing
            If FCInf.id = Nothing Then
                ErrString = "参数不正确"
                Return False
            End If
            sqlstr = "select * from Base_Water where id=" & FCInf.id
            ds = New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables(0).Rows.Count < 1 Then
                ErrString = "无法找到指定项目"
                Return False
            End If
            FCInf.FName = ds.Tables(0).Rows(0).Item("FName")
            FCInf.FCName = ds.Tables(0).Rows(0).Item("FCName")
            FCInf.FRate = ds.Tables(0).Rows(0).Item("FRate")
            FCInf.FBalance = ds.Tables(0).Rows(0).Item("FBalance")
            FCInf.FUse = ds.Tables(0).Rows(0).Item("FUse")
            Return True
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region

#Region "水费单_保存"
    Public Function _BILL_Water_SaveBill(ByVal FSession As String, ByRef FBinf As FBillWater, ByRef ErrString As String) As Boolean Implements IService._BILL_Water_SaveBill
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            Dim FUinf As New FUserInfo
            If GetUserInfoBySession(FSession, FUinf, ErrString) = False Then
                ErrString = FControl.GetErrString("保存单据失败!", ErrString)
                Return False
            End If
            '--权限
            Dim FisNew As Boolean = _User_CheckAuth(FSession, "BILL", "FWBILLNEW", ErrString) '新单据权限
            Dim FVMY As Boolean = _User_CheckAuth(FSession, "BILL", "FWBILLVERIFYMY", ErrString) '审核自己的单据
            Dim FVOther As Boolean = _User_CheckAuth(FSession, "BILL", "FWBILLVERIFY", ErrString) '审核其它人的单据
            Dim sqlstr As String = String.Empty
            Dim ds As DataSet = Nothing
            If FBinf.id > 0 Then
                '--审核
                If FBinf.FUid = FUinf.id Then
                    '-自己的单据
                    If FVMY = False Then
                        ErrString = "没有权限"
                        Return False
                    End If
                Else
                    '--其它人的单据
                    If FVOther = False Then
                        ErrString = "没有权限"
                        Return False
                    End If
                End If
                '--查询单据是否存在
                sqlstr = "select FBillNo from Bill_Water where id=" & FBinf.id
                If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                    ErrString = FControl.GetErrString("收费失败!", ErrString)
                    Return False
                End If
                If ds.Tables.Count < 1 Then
                    ErrString = FControl.GetErrString("收费失败!", "未找到指定的项目")
                    Return False
                End If
                If FBinf.FBillNo <> ds.Tables(0).Rows(0).Item("FBillNo") Then
                    ErrString = FControl.GetErrString("收费失败!", "类型不匹配")
                    Return False
                End If
                '--检查分组
                Dim FUids As String() = Nothing
                If GetUserPair(FUinf.id, FUids, ErrString) = False Then
                    FBinf.FVid = Nothing
                    FBinf.FVName = Nothing
                    Return False
                End If
                If FUids.Contains(FBinf.FUid) = False Then
                    ErrString = "与制单人不在同一组,无法执行收费操作!"
                    FBinf.FVid = Nothing
                    FBinf.FVName = Nothing
                    Return False
                End If
                '--审核
                sqlstr = String.Format("UPDATE Bill_Water set FVid={0},FVName='{1}' where id={2}", FUinf.id, FUinf.FRName, FBinf.id)
                FBinf.FVid = FUinf.id
                FBinf.FVName = FUinf.FRName
                Return DControl.ExecSQL(sqlstr, ErrString)
            Else
                '--新单
                '--检查客户和摘要
                FBinf.FCustom.FRate = FBinf.FRate
                FBinf.FCustom.FBalance = FBinf.FAmount + FBinf.FCost
                FBinf.FCustom.FUse = FBinf.FQty
                _BILL_Water_ManageBase(FSession, FBinf.FCustom, ErrString)
                FBinf.FUid = FUinf.id
                FBinf.FUName = FUinf.FRName
                If FisNew = False Then
                    ErrString = "没有权限"
                    Return False
                End If
                '--检查必填项
                If FBinf.FDate = Nothing Then
                    ErrString = "日期不能为空"
                    Return False
                End If
                If FBinf.FCustom.FName = Nothing Then
                    ErrString = "客户不能为空!"
                    Return False
                End If
                If FBinf.FCustom.id = 0 Then
                    ErrString = "客户不能为空!"
                    Return False
                End If
                If FBinf.FAmount = Nothing Then
                    ErrString = "金额不能为空!"
                    Return False
                End If

                '--单号,验证码
                Dim FBLSH As Integer = 0
                If FBinf.FBillNo Is Nothing Then
                    If GetBillMaxNumber(4, FUinf.FOrgid, FBLSH, ErrString) = False Then
                        ErrString = String.Format("取单据号失败!{0}错误信息:{1}", vbCrLf, ErrString)
                        Return False
                    End If
                    FBinf.FBillNo = FBinf.FDate.ToString("yyyyMMdd") & FBLSH.ToString("000000")
                    FBinf.FVerifyCode = Int(FBinf.FAmount * CDbl(FBLSH.ToString("0000")) / CDbl(FBinf.FDate.ToString("yyyyMMdd")) * 987654)
                End If
                If FBinf.FBCode Is Nothing Then
                    FBinf.FBCode = Guid.NewGuid.ToString.ToUpper
                End If
                '--开始保存
                sqlstr = String.Format("INSERT INTO Bill_Water(FCid,FName,FCName,FBillNo,FVerifyCode,FDate,FRDate,FLRate,FRate,FQty,FPrice,FAcc,FManageFee,FOther,FLBalance,FCost,FAmount,FRef,FUid,FUName,FBCode,FOrgid) values('{0}','{1}','{2}','{3}','{4}','{5}',GETDATE(),'{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}',{20})", FBinf.FCustom.id, FBinf.FCustom.FName, FBinf.FCustom.FCName, FBinf.FBillNo, FBinf.FVerifyCode, FBinf.FDate.ToString, FBinf.FLRate, FBinf.FRate, FBinf.FQty, FBinf.FPrice, FBinf.FAcc, FBinf.FManageFee, FBinf.FOther, FBinf.FLBalance, FBinf.FCost, FBinf.FAmount, FBinf.FRef, FBinf.FUid, FBinf.FUName, FBinf.FBCode, FUinf.FOrgid)
                If DControl.ExecSQL(sqlstr, ErrString) = False Then
                    ErrString = FControl.GetErrString("保存失败!", ErrString)
                    Return False
                End If
                '--取ID号
                sqlstr = String.Format("select id from Bill_Water where FBCode='{0}'", FBinf.FBCode)
                ds = New DataSet
                If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                    ErrString = FControl.GetErrString("取单据内码失败!", ErrString)
                    Return False
                End If
                If ds.Tables(0).Rows.Count < 1 Then
                    ErrString = FControl.GetErrString("取单据内码失败!", "未知错误")
                    Return False
                End If
                FBinf.id = ds.Tables(0).Rows(0).Item("id")
                FBinf.FUid = FUinf.id
                FBinf.FUName = FUinf.FRName
                Return True
            End If
        Catch ex As Exception
            ErrString = FControl.GetErrString("保存单据失败!", ex.Message)
            Return False
        End Try
    End Function
#End Region

#Region "水费单_获取单据"
    Public Function _BILL_Water_GetBillInfo(ByVal FSession As String, ByRef FBinf As FBillWater, ByRef ErrString As String) As Boolean Implements IService._BILL_Water_GetBillInfo
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            '--取单据ID
            If FBinf.id = Nothing Then
                ErrString = "参数不正确"
                Return False
            End If
            Dim sqlstr As String = "select * from Bill_Water where id=" & FBinf.id
            Dim ds As New DataSet
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables(0).Rows.Count < 1 Then
                ErrString = "未找到指定单据"
                Return False
            End If
            Dim r As DataRow = ds.Tables(0).Rows(0)
            FBinf.FCustom = New FBaseWater
            FBinf.FCustom.id = FControl.cvN(r.Item("FCid"))
            FBinf.FCustom.FName = FControl.cvN(r.Item("FName"))
            FBinf.FCustom.FCName = FControl.cvN(r.Item("FCName"))
            FBinf.FAcc = FControl.cvN(r.Item("FAcc"))
            FBinf.FAmount = FControl.cvN(r.Item("FAmount"))
            FBinf.FBillNo = FControl.cvN(r.Item("FBillNo"))
            FBinf.FCancel = FControl.cvN(r.Item("FCancel"))
            FBinf.FCost = FControl.cvN(r.Item("FCost"))
            FBinf.FDate = FControl.cvN(r.Item("FDate"))
            FBinf.FLBalance = FControl.cvN(r.Item("FLBalance"))
            FBinf.FRDate = FControl.cvN(r.Item("FRDate"))
            FBinf.FLRate = FControl.cvN(r.Item("FLRate"))
            FBinf.FRate = FControl.cvN(r.Item("FRate"))
            FBinf.FManageFee = FControl.cvN(r.Item("FManageFee"))
            FBinf.FOther = FControl.cvN(r.Item("FOther"))
            FBinf.FPCount = FControl.cvN(r.Item("FPCount"))
            FBinf.FPrice = FControl.cvN(r.Item("FPrice"))
            FBinf.FQty = FControl.cvN(r.Item("FQty"))
            FBinf.FUid = FControl.cvN(r.Item("FUid"))
            FBinf.FRef = FControl.cvN(r.Item("FRef"))
            FBinf.FUName = FControl.cvN(r.Item("FUName"))
            FBinf.FVid = FControl.cvN(r.Item("FVid"))
            FBinf.FVName = FControl.cvN(r.Item("FVName"))
            FBinf.FVerifyCode = FControl.cvN(r.Item("FVerifyCode"))
            FBinf.FOrgid = FControl.cvN(r.Item("FOrgid"))
            Return True
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region

#Region "水费单_查询单据"
    Public Function _BILL_Water_GetBillList(ByVal FSession As String, ByVal FSDate As Date, ByVal FEDate As Date, ByVal FName As String, ByRef dsArr As Byte(), ByRef ErrString As String) As Boolean Implements IService._BILL_Water_GetBillList
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            Dim sqlstr As String = String.Empty
            Dim ds As DataSet = Nothing
            '--权限
            Dim FUinf As New FUserInfo
            If GetUserInfoBySession(FSession, FUinf, ErrString) = False Then
                Return False
            End If
            DControl.ExecSQL("update t set t.FOrgName=t1.FName from Bill_Water t,Base_Org t1 where t.FOrgid=t1.id", "")
            sqlstr = String.Format("select id,FName '单位',FCName '交款人',FBillNo '单号',FQty '用水量',FAmount '本次交款',FRef '参考走表',FUName '制单',FVName '收费',FPCount '打印次数',FOrgName '收款单位' from Bill_Water where FDate between '{0}' and '{1}'", FSDate.ToString("yyyy-MM-dd 00:00:00"), FEDate.ToString("yyyy-MM-dd 23:59:59"))
            If FName <> String.Empty Then
                sqlstr += String.Format(" and FName like '%{0}%'", FName)
            End If
            If _User_CheckAuth(FSession, "BILL", "FWBILLVIEW", ErrString) = False Then
                '--自己
                If _User_CheckAuth(FSession, "BILL", "FWBILLVERIFY", ErrString) = False Then
                    '--没审核权
                    sqlstr += "and FUid=" & FUinf.id
                Else
                    '--查看全部

                End If
            Else
                '--全部
            End If
            '--制单单位
            Dim FOrg As String = Nothing
            If FUinf.id = 1 Then
                FOrg = ""
            Else
                FOrg = " and FOrgid=" & FUinf.FOrgid
            End If
            sqlstr += FOrg
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            dsArr = FControl.CvtDs(ds)
            Return True
        Catch ex As Exception
            ErrString = ex.Message
            Return False
        End Try
    End Function
#End Region

#Region "水费单_增加打印次数"
    Public Function _Bill_Water_AddPrintCount(ByVal FSession As String, ByRef FBInf As FBillWater, ByRef ErrString As String) As Boolean Implements IService._Bill_Water_AddPrintCount
        If FVerifyUser(FSession, ErrString) = False Then
            Return False
        End If
        Try
            If FBInf Is Nothing Then
                ErrString = "参数不正确!"
                Return False
            End If
            If FBInf.id = 0 Then
                ErrString = "参数不正确"
                Return False
            End If
            Dim FUinf As New FUserInfo
            If GetUserInfoBySession(FSession, FUinf, ErrString) = False Then
                Return False
            End If
            Dim sqlstr As String = "update Bill_Water set FPCount=FPCount+1 output inserted.FPCount where FCancel=0 and id=" & FBInf.id
            Dim ds As New DataSet
            If FBInf.FVid <> FUinf.id Then
                '--打印其他人审核单据,检查权限
                ErrString = "只能打印自己收费的单据"
                Return False
            Else
                '--打印自己审核的单据
            End If
            If DControl.QuerySQL(sqlstr, ds, ErrString) = False Then
                Return False
            End If
            If ds.Tables.Count < 1 Then
                ErrString = FControl.GetErrString("打印失败!", "内部服务器错误")
                Return False
            End If
            If ds.Tables(0).Rows.Count < 1 Then
                ErrString = FControl.GetErrString("打印失败", "无法找到相应单据")
                Return False
            End If
            FBInf.FPCount = ds.Tables(0).Rows(0).Item("FPCount")
            DControl.WriteSysLog("BILL", FUinf.id, FUinf.FName, "打印单据", String.Format("打印水费单据 单据号:{0}", FBInf.FBillNo), 0)
            Return True
        Catch ex As Exception
            FControl.GetErrString("打印失败!", ex.Message)
            Return False
        End Try
    End Function
#End Region
End Class
